package com.xuanyuanxinyu.view

import android.graphics.Bitmap
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import androidx.core.view.ViewCompat
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide

/**
 * @FileName:
 * @Description:
 * @author ken.luo
 * @date 2023/4/4
 * @version V1.0
 * @email 424533553@qq.com
 */
fun View.click(block : () -> Unit) {
    setOnClickListener {
        block()
    }
}

fun View.visible() {
    visibility = View.VISIBLE
}

fun View.gone() {
    visibility = View.GONE
}

fun ImageView.load(url : String) {
    Glide.with(this).load(url).into(this)
}

fun View.background(drawable : Drawable) {
    ViewCompat.setBackground(this,drawable)
}

object ViewAdapter {
    @JvmStatic
    @BindingAdapter("srcDrawableId")
    fun setSrcDrawableId(view : ImageView,drawableId : Int) {
        view.setImageResource(drawableId)
    }
    
    @JvmStatic
    @BindingAdapter("srcBitmap")
    fun setSrcBitmap(view : ImageView,bitmap : Bitmap) {
        view.setImageBitmap(bitmap)
    }
    
    @JvmStatic
    @BindingAdapter("isVisible")
    fun setVisible(view : View,visible : Boolean) {
        view.visibility = if (visible) View.VISIBLE else View.GONE
    }
    
    @JvmStatic
    @BindingAdapter("viewSelected")
    fun setSelected(view : View,selected : Boolean) {
        view.isSelected = selected
    }
    
    @JvmStatic
    @BindingAdapter("intText")
    fun setIntText(view : TextView,value : Int) {
        view.text = value.toString()
    }
    
    @JvmStatic
    @BindingAdapter("fontStyle")
    fun setTextStyle(view : TextView,fontStyle : Int) {
        view.typeface = Typeface.defaultFromStyle(fontStyle)
    }
}